package cn.itamt.slideshow {
	import flash.net.URLLoaderDataFormat;	
	
	import cn.itamt.ui.base.TSprite;
	
	import com.greensock.TweenMax;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.StageScaleMode;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;	

	/**
	 * @author tamt
	 */
	public class Wwad extends TSprite {
		public static const STAGE_W : Number = 600;
		public static const STAGE_H : Number = 200;

		var imgUrl : String;
		var imgLoader : Loader;
		var countDownTimer : Timer;
		var loadingBar : *;

		//倒计时时间文本框
		public var d_mc : MovieClip;		public var h_mc : MovieClip;		public var m_mc : MovieClip;		public var s_mc : MovieClip;
		var time : Date;
		var link : String;
		
		private var configXML:XML;
		var xmlLoader:URLLoader;

		public function Wwad() {
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			super();
			
			link = this.root.loaderInfo.parameters.link ? this.root.loaderInfo.parameters.link : 'http://www.baidu.com';
			
			var time_str : String = this.root.loaderInfo.parameters.time ? this.root.loaderInfo.parameters.time : '2009/11/31/23/36/30';
			var t_arr : Array = time_str.split('/');
			time = new Date();
			time.setFullYear(int(t_arr[0]), int(t_arr[1]) - 1, int(t_arr[2]));
			time.setHours(int(t_arr[3]), int(t_arr[4]), int(t_arr[5]));
			
			countDownTimer = new Timer(1000);
			countDownTimer.addEventListener(TimerEvent.TIMER, onTimer);
			countDownTimer.start();
			onTimer();
			
			imgUrl = this.root.loaderInfo.parameters.img ? this.root.loaderInfo.parameters.img : 'http://image-001.yo2cdn.com/wp-content/uploads/272/27247/2009/05/interactivepng.png';
			imgLoader = new Loader();
			imgLoader.load(new URLRequest(imgUrl));
			addChildAt(imgLoader, 0);
			imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
			imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			
			loadingBar = new (getDefinitionByName('LoadingBar') as Class)();
			loadingBar.x = STAGE_W / 2;
			loadingBar.y = STAGE_H / 2;
			addChild(loadingBar);
			
			this.buttonMode = true;
			this.addEventListener(MouseEvent.CLICK, navToLink);
			
			xmlLoader = new URLLoader();
			xmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			xmlLoader.addEventListener(Event.COMPLETE, onLoadConfig);
			xmlLoader.load(new URLRequest('config.xml'));
		}
		
		private function onLoadConfig(evt:Event):void{
			this.configXML = new XML(xmlLoader.data);
			
			var time_str : String = this.configXML.time?this.configXML.time:'2009/11/31/23/36/30';
			var t_arr : Array = time_str.split('/');
			time = new Date();
			time.setFullYear(int(t_arr[0]), int(t_arr[1]) - 1, int(t_arr[2]));
			time.setHours(int(t_arr[3]), int(t_arr[4]), int(t_arr[5]));
			
			imgLoader.load(new URLRequest(this.configXML.img));
			this.link = this.configXML.link;
		}

		private function navToLink(evt : MouseEvent) : void {
			navigateToURL(new URLRequest(this.link), '_blank');
		}

		var ld : int, lh : int, lm : int, ls : int;

		private function onTimer(evt : TimerEvent = null) : void {
			var cTime : Date = new Date();
			var ms : Number = (time.time - cTime.time);
			var ts : int = int(ms / 1000);
			var d : int = int(ts / (60 * 60 * 24));
			var h : int = int((ts % (60 * 60 * 24)) / (60 * 60));
			var m : int = int((ts / 60) % 60);
			var s : int = int(ts % 60);

			d_mc.d_tf.text = format(d);
			h_mc.h_tf.text = format(h);
			m_mc.m_tf.text = format(m);
			s_mc.s_tf.text = format(s);
			
			if(d != ld) {
				ld = d;
				TweenMax.from(d_mc, .5, {colorTransform:{exposure:2}});
				TweenMax.from(d_mc, .5, {glowFilter:{color:0xffffff, alpha:1, blurX:4, blurY:4}});
			}
			
			if(h != lh) {
				lh = h;
				TweenMax.from(h_mc, .5, {colorTransform:{exposure:2}});
				TweenMax.from(h_mc, .5, {glowFilter:{color:0xffffff, alpha:1, blurX:4, blurY:4}});
			}
			
			if(m != lm) {
				lm = m;
				TweenMax.from(m_mc, .5, {colorTransform:{exposure:2}});
				TweenMax.from(m_mc, .5, {glowFilter:{color:0xffffff, alpha:1, blurX:4, blurY:4}});
			}
			
			if(s != ls) {
				ls = s;
				TweenMax.from(s_mc, .5, {colorTransform:{exposure:2}});
				TweenMax.from(s_mc, .5, {glowFilter:{color:0xffffff, alpha:1, blurX:4, blurY:4}});
			}
		}

		private function format(value : Number) : String {
			return value < 10 ? ('0' + value) : value.toString();
		}

		private function onIOError(evt : ErrorEvent) : void {
			var tf : TextField = new TextField();
			tf.autoSize = 'left';
			tf.text = '加载图片错误。';
			addChild(tf);
		}

		private function onLoadComplete(evt : Event) : void {
			this.removeChild(this.loadingBar);
			
			/*
			this.imgLoader.scaleX = this.imgLoader.scaleY = 1;
			if(this.imgLoader.width > STAGE_W || this.imgLoader.height > STAGE_H) {
				this.imgLoader.scaleX = this.imgLoader.scaleY = Math.min(STAGE_W / this.imgLoader.width, STAGE_H / this.imgLoader.height);
			}
			
			this.imgLoader.x = STAGE_W / 2 - this.imgLoader.width / 2;
			this.imgLoader.y = STAGE_H / 2 - this.imgLoader.height / 2;
			 * 
			 */
		}

		/**
		 * 加载进度
		 */
		private function onLoading(evt : ProgressEvent) : void {
			var percent : int = int(100 * (evt.bytesLoaded / evt.bytesTotal));
			loadingBar.progress_tf.text = percent < 10 ? ('0' + percent) : percent.toString();
		}
	}
}
